package org.projsquirrel;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.*;
import android.util.Log;
import android.view.*;
import android.location.*;

@SuppressWarnings("unused")
public class SurveyActivity extends Activity
{
	private Context localInstance;
    private TableRow tableTime, tableDate;
    private TextView textTime, textDate, textSqr1, textSqr2;
    private EditText textZip;
    private int hour, minute, Year, month, day;
    static final int MAP_ACTIVITY = 100;
    static final int TIME_DIALOG_ID = 0;
    static final int DATE_DIALOG_ID = 1;
    static final int EMAIL_DIALOG_ID = 2;
    static final int DELTA_DIALOG_ID = 3;
    static final int THISSITE_DIALOG_ID = 4;
    static final int OTHERSITE_DIALOG_ID = 5;
    static final int SITE_DIALOG_ID = 6;
    static final int MAP_DIALOG_ID = 7;
	private static final int SUBMIT = 8;
    
    public LocationManager locationManager;
    private Canvas imagecanvas;
    private Button myButton;
    
    //Your Opinion...
    private TableRow tableEmail, tableDeltaSquirrel, tableThisSite, tableOtherSite, tableComments;
    private TextView email, deltaSquirrel, thisSite, otherSite, comments, titleEmail;
    private Button submit;
    private String s1[] = {"    Yes    ", "     No    ", " Unsure "};
    private String s2[] = {"   Seldom   ", "   Often   ", "Regularly", "   Never   "};
    private String s3[] = {"   Low   ", "Medium", "  High  ", "   Never   "};
    private int a=0, b=0, C=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0;
    private TextView answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, 
    	answer11, answer12, answer13;
    private ImageButton buttonUp1, buttonDown1, buttonUp2, buttonDown2, buttonUp3, buttonDown3, buttonUp4, buttonDown4,
    	buttonUp5, buttonDown5, buttonUp6, buttonDown6, buttonUp7, buttonDown7, buttonUp8, buttonDown8, buttonUp9,
    	buttonDown9, buttonUp10,  buttonDown10, buttonUp11, buttonDown11, buttonUp12, buttonDown12, buttonUp13, buttonDown13,
    	sqr1_buttonUp, sqr1_buttonDown, sqr2_buttonUp, sqr2_buttonDown;
    private Spinner spinner;

    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.survey);
       // TableRow NewRow = new TableRow( localInstance, null); This line breaks stuff. -5 internets
        localInstance = this;
        
        //location manager initialization
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        //link all variables to the View
		tableTime = (TableRow)findViewById(R.id.table_time);
        tableDate = (TableRow)findViewById(R.id.table_date);
        textTime = (EditText)findViewById(R.id.text_time);
        textDate = (EditText)findViewById(R.id.text_date);
        textZip = (EditText)findViewById(R.id.text_zip);
        textSqr1 = (EditText)findViewById(R.id.text_sqr1);
        textSqr2 = (EditText)findViewById(R.id.text_sqr2);
       
        tableEmail = (TableRow)findViewById(R.id.table_email);
        tableDeltaSquirrel = (TableRow)findViewById(R.id.table_deltaSquirrel);
        tableThisSite = (TableRow)findViewById(R.id.table_thisSite);
        tableOtherSite = (TableRow)findViewById(R.id.table_otherSite);
        email = (TextView)findViewById(R.id.answer_email);
        deltaSquirrel = (TextView)findViewById(R.id.answer_deltaSquirrel);
        thisSite = (TextView)findViewById(R.id.answer_thisSite);
        otherSite = (TextView)findViewById(R.id.answer_otherSite);
        titleEmail = (TextView)findViewById(R.id.text_email);
        spinner = (Spinner)findViewById(R.id.spinner);
        
     // ANSWERS FOR OPTIONS
        answer1 = (TextView)findViewById(R.id.Text);
        answer2 = (TextView)findViewById(R.id.Text1);
        answer3 = (TextView)findViewById(R.id.Text2);
        answer4 = (TextView)findViewById(R.id.Text3);
        answer5 = (TextView)findViewById(R.id.Text4);
        answer6 = (TextView)findViewById(R.id.Text5);
        answer7 = (TextView)findViewById(R.id.Text6);
        answer8 = (TextView)findViewById(R.id.Text7); 
        answer9 = (TextView)findViewById(R.id.Text8);
        answer10 = (TextView)findViewById(R.id.Text9);
    	answer11 = (TextView)findViewById(R.id.Text10); 
    	answer12 = (TextView)findViewById(R.id.Text11); 
    	answer13 = (TextView)findViewById(R.id.Text12);
        
        // UP/DOWN ARROWS FOR OPTIONS
        buttonUp1 = (ImageButton)findViewById(R.id.btnUp);
        buttonDown1 = (ImageButton)findViewById(R.id.btnDwn);
        buttonUp2 = (ImageButton)findViewById(R.id.btnUp1);
        buttonDown2 = (ImageButton)findViewById(R.id.btnDwn1);
        buttonUp3 = (ImageButton)findViewById(R.id.btnUp2);
        buttonDown3 = (ImageButton)findViewById(R.id.btnDwn2);
        buttonUp4 = (ImageButton)findViewById(R.id.btnUp3);
        buttonDown4 = (ImageButton)findViewById(R.id.btnDwn3);
        buttonUp5 = (ImageButton)findViewById(R.id.btnUp4);
        buttonDown5 = (ImageButton)findViewById(R.id.btnDwn4);
        buttonUp6 = (ImageButton)findViewById(R.id.btnUp5);
        buttonDown6 = (ImageButton)findViewById(R.id.btnDwn5);
        buttonUp7 = (ImageButton)findViewById(R.id.btnUp6);
        buttonDown7 = (ImageButton)findViewById(R.id.btnDwn6);;
        buttonUp8 = (ImageButton)findViewById(R.id.btnUp7);
        buttonDown8 = (ImageButton)findViewById(R.id.btnDwn7);
        buttonUp9 = (ImageButton)findViewById(R.id.btnUp8);
        buttonDown9 = (ImageButton)findViewById(R.id.btnDwn8);
        buttonUp10 = (ImageButton)findViewById(R.id.btnUp9);
        buttonDown10 = (ImageButton)findViewById(R.id.btnDwn9);
        buttonUp11 = (ImageButton)findViewById(R.id.btnUp10);
        buttonDown11 = (ImageButton)findViewById(R.id.btnDwn10);
        buttonUp12 = (ImageButton)findViewById(R.id.btnUp11);
        buttonDown12 = (ImageButton)findViewById(R.id.btnDwn11);
        buttonUp13 = (ImageButton)findViewById(R.id.btnUp12);
        buttonDown13 = (ImageButton)findViewById(R.id.btnDwn12);
        sqr1_buttonUp = (ImageButton)findViewById(R.id.sqr1_btnUp);
        sqr1_buttonDown = (ImageButton)findViewById(R.id.sqr1_btnDwn); 
        sqr2_buttonUp = (ImageButton)findViewById(R.id.sqr2_btnUp);
        sqr2_buttonDown = (ImageButton)findViewById(R.id.sqr2_btnDwn);
        
        submit = (Button)findViewById(R.id.button_submit);
        
        final Calendar c = Calendar.getInstance();
        
        //set the defaults for the time
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        
        //set if its AM or PM
        String amPm;
        if(hour > 11)
        	amPm = "PM";
        else
        	amPm = "AM";
        
        //format the hours
        if(hour > 12)
        	hour -= 12;
        if(hour == 0)
        	hour = 12;
        
        //format the minutes
        String zero;
        if(minute < 10)
        	zero = "0";
        else
        	zero = "";
        
        //set the textTime            
        textTime.setText(hour + ":" + zero + minute + " " + amPm);
        
        //set the defaults for the date
        Year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        textDate.setText(month + 1 + "/" + day + "/" + Year);
                
		//set default zip code
        /*
		Location current = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		Geocoder geo = new Geocoder(this,Locale.getDefault());
		List<Address> addresses;
		try {
			addresses = geo.getFromLocation(current.getLatitude(), current.getLongitude(), 1);
			if(addresses != null && addresses.get(0).getPostalCode() != null)
				textZip.setText(addresses.get(0).getPostalCode());
			else
				textZip.setText("Select location from map");
		} catch (IOException e) {
			// 
			e.printStackTrace();
		}	
		*/
    
        //set the default for Your Opinion section
         deltaSquirrel.setText("");
         email.setText("");
         thisSite.setText("");
         otherSite.setText("");
      
        //link the On Click Events to the TextViews
        tableTime.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		}); 
        tableDate.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});
        textZip.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				showDialog(MAP_DIALOG_ID);
			}
		});
 
        tableDeltaSquirrel.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		showDialog(DELTA_DIALOG_ID);
        	}
        });
        
        tableThisSite.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		showDialog(THISSITE_DIALOG_ID);
        	}
        });
        
        tableOtherSite.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		showDialog(OTHERSITE_DIALOG_ID);
        	}
        });
        submit.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		createReport();
        	}
        });
        
    ////NUMBER OF SQUIRRELS SECTION////
        sqr1_buttonUp.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	String temp = textSqr1.getText().toString();
            	int value = Integer.parseInt( temp );
            	if(value > 0){
            		value--;
            		temp = Integer.toString(value);
            		textSqr1.setText(temp);
            	}
            }
        });
        sqr1_buttonDown.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	String temp = textSqr1.getText().toString();
            	int value = Integer.parseInt( temp );
            	value++;
            	temp = Integer.toString(value);
            	textSqr1.setText(temp);
            }
        });
        sqr2_buttonUp.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	String temp = textSqr2.getText().toString();
            	int value = Integer.parseInt( temp );
            	if(value > 0){
            		value--;
            		temp = Integer.toString(value);
            		textSqr2.setText(temp);
            	}
            }
        });
        sqr2_buttonDown.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	String temp = textSqr2.getText().toString();
            	int value = Integer.parseInt( temp );
            	value++;
            	temp = Integer.toString(value);
            	textSqr2.setText(temp);
            }
        });
        
        ////TREES CATEGORY////
        // tiny seeded trees
        buttonUp1.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	
            	a--;
                if(a<0)
                	a=2;
                answer1.setText(s1[a]);
            }
        });
        buttonDown1.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer1.setText(s1[a]);
            	a++;
            	if(a>2)
            		a=0;
            	
            }
        });
        
        // nut bearing trees
        buttonUp2.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	
            	b--;
                if(b<0)
                	b=2;  
                answer2.setText(s1[b]);
            }
        });
        buttonDown2.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer2.setText(s1[b]);
            	b++;
            	if(b>2)
            		b=0;
            	
            }
        });
        
        // fruit bearing trees
        buttonUp3.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                
            	C--;
                if(C<0)
                	C=2;
                answer3.setText(s1[C]);
            }
        });
        buttonDown3.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	answer3.setText(s1[C]);
            	C++;
            	if(C>2)
            		C=0;
            	
            }
        });
        
        // coniferous trees
        buttonUp4.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	
            	d--;
                 if(d<0)
                 	d=2;
                answer4.setText(s1[d]); 
            }
        });
        buttonDown4.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer4.setText(s1[d]);
            	d++;
            	if(d>2)
            		d=0;
            	 
            }
        });
        
        ////FOOD CATEGORY////
        // bird feeders
        buttonUp5.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	
            	e--;
                if(e<0)
                	e=3;
                answer5.setText(s2[e]);
            }
        });
        buttonDown5.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {  	 
            	answer5.setText(s2[e]);
            	e++;
            	if(e>3)
            		e=0;
                
            }
        });
        
        // human handouts
        buttonUp6.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                
            	f--;
                if(f<0)
                	f=3;
                answer6.setText(s2[f]);
            }
        });
        buttonDown6.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer6.setText(s2[f]);
            	f++;
            	if(f>3)
            		f=0;
            	
            }
        });
        
        // garbage
        buttonUp7.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                 
            	g--;
                 if(g<0)
                 	g=3;
                answer7.setText(s2[g]);
            }
        });
        buttonDown7.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer7.setText(s2[g]);
            	g++;
            	if(g>3)
            		g=0;
            	
            }
        });
        
        // trees/other plants
        buttonUp8.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
              
            	h--;
                 if(h<0)
                 	h=3;
                answer8.setText(s2[h]); 
            }
        });
        buttonDown8.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {           	
            	answer8.setText(s2[h]);
            	h++;
            	if(h>3)
            		h=0;
            	
            }
        });
        
        //PREDATOR CATEGORY
        //dogs
        buttonUp9.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
               
            	i--;
                if(i<0)
                	i=3;
                answer9.setText(s3[i]);
            }
        });
        buttonDown9.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer9.setText(s3[i]);
            	i++;
            	if(i>3)
            		i=0;
            	
            }
        });
        
        //cats
        buttonUp10.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                
            	j--;
                if(j<0)
                	j=3;
                answer10.setText(s3[j]);
            }
        });
        buttonDown10.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer10.setText(s3[j]);
            	j++;
            	if(j>3)
            		j=0;
            	
            }
        });
        
        // coyotes
        buttonUp11.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                 
            	 k--;
                 if(k<0)
                 	k=3;
                 answer11.setText(s3[k]);
            }
        });
        buttonDown11.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	answer11.setText(s3[k]);
            	k++;
            	if(k>3)
            		k=0;
            	
            }
        });
        
        //hawks
        buttonUp12.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                
            	l--;
                if(l<0)
                	l=3;
                answer12.setText(s3[l]);
            }
        });
        buttonDown12.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	answer12.setText(s3[l]);
            	l++;
            	if(l>3)
            		l=0;
            	
            }
        });
        
        // poisoned grain
        buttonUp13.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
              
            	m--;
                if(m<0)
                	m=3;
                answer13.setText(s3[m]);
            }
        });
        buttonDown13.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	answer13.setText(s3[m]);
            	m++;
            	if(m>3)
            		m=0;
            	
            }
        });

   	}
    
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.survey_menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.reset:
	        //resetSurvey();
	        return true;
	    case R.id.surveyAbout:
	        //showHelp();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	if(requestCode == MAP_ACTIVITY && resultCode == RESULT_OK) //the map activity has returned and it supplied a new location
    	{
    		textZip.setText(data.getCharSequenceExtra("zip"));    		
    	}
    }    
    
    
	protected void createReport() 
	{
		//boolean expression that maintains whether or not to exit w/ errors
		boolean exitNow = false;
		
		//regular expressions to find if a valid email address
    	boolean b = Pattern.matches("[\\w+.-]+@[\\w\\.]+", email.getText());
    	if( b) //value matches a valid email address
    	{
    		titleEmail.setTextColor(Color.WHITE);
    		titleEmail.setText("What is your email?");
    		//we need to make a dialog here instead.
    		//Dialog submitDialog = new Dialog();
    		
    	}
    	else
    	{
    		titleEmail.setTextColor(Color.RED);
    		titleEmail.setText("What is your email?\nFormat as xxx@bla.bla or xxx+xx@bla.bla");
    		exitNow = true;
    	}
    	
    	TextView titleZip = (TextView)findViewById(R.id.title_zip);
    	if( textZip.getText().toString() == "")
    	{
    		titleZip.setTextColor(Color.RED);
    		titleZip.setText("5 Digit Zip Code");
    		exitNow = true;	
    	}
    	else
    	{
	    	titleZip.setTextColor(Color.WHITE);
			titleZip.setText("Zip Code");
    	}
    	
    	if( exitNow)
    	{
    		Toast.makeText(getApplicationContext(), "Red Item(s) are Invalid. Format Correctly and Submit again.", Toast.LENGTH_LONG).show();
    		return;
    	}
    	else
    	{
    		showDialog(SUBMIT);
    	}
    	
    	//Collects all of the information in string
    	String csv = "";
    	String empty = "";
    	 	 
    		DateFormat formatter = new SimpleDateFormat("M/dd/yyyy h:mm a");
    		Date obsdate = new Date();
    		try {
				obsdate = formatter.parse( textDate.getText().toString() + " " + textTime.getText().toString() );
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		Calendar obsCal = Calendar.getInstance();
    		obsCal.setTime(obsdate);
    		
    	//Day
    	csv += "Day," + obsCal.get(Calendar.DAY_OF_MONTH) + "\n";
    	//Month
    	csv += "Month," + (obsCal.get(Calendar.MONTH) +1) + "\n";
    	//Year
    	csv += "Year," + obsCal.get(Calendar.YEAR) + "\n";
    	//Hour
    	//Minute
    	//AMPM
    	int hour = obsCal.get(Calendar.HOUR_OF_DAY);
    	if( hour <= 12 )
    	{
    		csv += "Hour," + hour + "\n";
    		csv += "Minute," + obsCal.get(Calendar.MINUTE) + "\n";
    		csv += "AMPM," + "AM" + "\n";
    	}
    	else
    	{
    		csv += "Hour," + (hour - 12) + "\n";
    		csv += "Minute," + obsdate.getMinutes() + "\n";
    		csv += "AMPM," + "PM" + "\n";
    	}
    	//ZIP
    	csv += "ZIP," + textZip.getText().toString() + "\n";
    	
//    	LATITUDE
    	csv += "LATITUDE," + "null" + "\n";
//    	LONGITUDE
    	csv += "LONGITUDE," + "null" + "\n";
//    	EMAIL
    	csv += "EMAIL," + email.getText() + "\n";
//    	NUM_GRAY_SQUIRRELS
    	csv += "NUM_GRAY_SQUIRRELS" + textSqr1.getText().toString() + "\n";
//    	NUM_FOX_SQUIRRELS
    	csv += "NUM_FOX_SQUIRRELS," + textSqr2.getText().toString() + "\n";
//    	SETTING
    	String[] items=getResources().getStringArray(R.array.location_type);
    	String item = items[ spinner.getSelectedItemPosition() ];
    		if( item == "Cemetery")
    			csv += "SETTING," + "CEMETERY" + "\n";

    		else if( item ==  "Commercial Establishment")
    			csv += "SETTING," + "COMMER" + "\n";

    		else if( item ==  "Farmland")
    			csv += "SETTING," + "FARM" + "\n";

    		else if( item ==  "Forest or Nature Preserve")
    			csv += "SETTING," + "NATURE" + "\n";

    		else if( item ==  "Highrise Building")
    			csv += "SETTING," + "HIGHRISE" + "\n";

    		else if( item ==  "Neighborhood Park")
    			csv += "SETTING," + "PARK" + "\n";

    		else 	if( item ==  "Multiple Family Dwelling (e.g. Apartment Building)")
    			csv += "SETTING," + "MULT_FAM" + "\n";

    		else if( item ==  "School, College or University Campus")
    			csv += "SETTING," + "SCHOOL" + "\n";

    		else if( item ==  "Single Family House or Duplex")
    			csv += "SETTING," + "SINGLE_FAM" + "\n";

    		else if( item ==  "Vacant Lot")
    			csv += "SETTING," + "LOT" + "\n";

    		else if( item ==  "Zoo, Arboretum or Botanical Garden")
    			csv += "SETTING," + "GARDEN" + "\n";
//    	NUT_TREES
    	csv += "NUT_TREES," + answer2.getText().toString().toUpperCase() + "\n";
//    	SEED_TREES
    	csv += "SEED_TREES," + empty + "\n";
//    	FRUIT_TREES
    	csv += "FRUIT_TREES," + answer3.getText().toString().toUpperCase() + "\n";
//    	TINY_TREES
    	csv += "TINY_TREES," + answer1.getText().toString().toUpperCase() + "\n";
//    	CON_TREES
    	csv += "CON_TREES," + answer4.getText().toString().toUpperCase() + "\n";
//    	OTHER_TREES
    	csv += "OTHER_TREES," + empty + "\n";
//    	FEED_BIRD_FEEDER
    	csv += "FEED_BIRD_FEEDER," + answer5.getText().toString().toUpperCase() + "\n";
//    	FEED_HANDOUTS
    	csv += "FEED_HANDOUTS," + answer6.getText().toString().toUpperCase() + "\n";
//    	FEED_GARBAGE
    	csv += "FEED_GARBAGE," + answer7.getText().toString().toUpperCase() + "\n";
//    	FEED_TREES
    	csv += "FEED_TREES," + answer8.getText().toString().toUpperCase() + "\n";
//    	FEED_OTHER
    	csv += "FEED_OTHER," + empty + "\n";
//    	FEED_OTHER_DETAILS
    	csv += "FEED_OTHER_DETAILS," + empty + "\n";
//    	NUM_SQUIRREL_CHANGE
    	if(deltaSquirrel.getText().toString() == "Stayed the Same" )
    	{
    		csv += "NUM_SQUIRREL_CHANGE," + "SAME" + "\n";
    	}
    	else
    	{
    		csv += "NUM_SQUIRREL_CHANGE," + deltaSquirrel.getText().toString().toUpperCase() + "\n";
    	}
//    	SITE_DOGS
    	csv += "SITE_DOGS," + answer9.getText().toString().toUpperCase() + "\n";
//    	SITE_CATS
    	csv += "SITE_CATS," + answer10.getText().toString().toUpperCase() + "\n";
//    	SITE_COYOTES
    	csv += "SITE_COYOTES," + answer11.getText().toString().toUpperCase() + "\n";
//    	SITE_HAWKS
    	csv += "SITE_HAWKS," + answer12.getText().toString().toUpperCase() + "\n";
//    	SITE_GRAIN
    	csv += "SITE_GRAIN," + answer13.getText().toString().toUpperCase() + "\n";
//    	THIS_NEW_SITE
    	csv += "THIS_NEW_SITE," + thisSite.getText().toString().toUpperCase() + "\n";
//    	USED_DIFFERENT_SITE
    	csv += "USED_DIFFERENT_SITE," + otherSite.getText().toString().toUpperCase() + "\n";
//    	COMMENTS
    	csv += "COMMENTS," + "null" + "\n";
    	
    	Log.i("Project_2_Group_2", csv);
	}	

    protected Dialog onCreateDialog(int id)
    {
    	switch(id)
    	{
    	case SUBMIT:
    		final CharSequence[] dialog_items = {"OK"};
    		AlertDialog.Builder submission = new AlertDialog.Builder(this);
    		submission.setTitle("Thank You for your submission.");
    		submission.setItems(dialog_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	    	if(item == 1) //MAP
	    	    	{
	    	    		Toast.makeText(getApplicationContext(), "Reset Survey", Toast.LENGTH_SHORT).show();
	    	    	}
	    	    }
	    	});
	    	AlertDialog submissions = submission.create();
	    	return submissions;
    	case TIME_DIALOG_ID:
    		return new TimePickerDialog(this,mTimeSetListener, hour, minute, false);
    	case DATE_DIALOG_ID:
    		return new DatePickerDialog(this,mDateSetListener, Year, month, day);
    	case EMAIL_DIALOG_ID:    		
    		break;
    	case MAP_DIALOG_ID:
	    	final CharSequence[] map_items = {"Keyboard", "Map"};
	    	AlertDialog.Builder map_builder = new AlertDialog.Builder(this);
	    	map_builder.setTitle("Edit ZIP code via");
	    	map_builder.setItems(map_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	    	if(item == 1) //MAP
	    	    	{
	    				Intent intent = new Intent(localInstance, Map.class);
	    				startActivityForResult(intent, MAP_ACTIVITY);	    	    		
	    	    	}
	    	    	else //KEYBOARD
	    	    	{
	    	    		
	    	    	}
	    	    	//textZip.setText(map_items[item]);
	    	    }
	    	});
	    	AlertDialog map_alert = map_builder.create();
	    	return map_alert;
    		

	    case DELTA_DIALOG_ID:
	    	final CharSequence[] delta_items = {"Increased", "Decreased", "Stayed the Same"};
	    	AlertDialog.Builder delta_builder = new AlertDialog.Builder(this);
	    	delta_builder.setTitle("Number has");
	    	delta_builder.setItems(delta_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	        //Toast.makeText(getApplicationContext(), delta_items[item], Toast.LENGTH_SHORT).show();
	    	    	deltaSquirrel.setText(delta_items[item]);
	    	    }
	    	});
	    	AlertDialog delta_alert = delta_builder.create();
	    	return delta_alert;
	    //Alternate Multiple Choice
	    case SITE_DIALOG_ID:
	    	final CharSequence[] site_items = {"This Location", "Other Locations"};
	    	AlertDialog.Builder site_builder = new AlertDialog.Builder(this);
	    	site_builder.setTitle("Have logged for");
	    	site_builder.setMultiChoiceItems(site_items, null, new DialogInterface.OnMultiChoiceClickListener() {
	    	    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
	    	    	//Needs logic to display based on what the site text already is
	    	       // Toast.makeText(getApplicationContext(), site_items[which], Toast.LENGTH_SHORT).show();
	    	    	thisSite.setText(site_items[which]);	
	    	    }
	    	});
	    	AlertDialog site_alert = site_builder.create();
	    	return site_alert;
	    	
	    case OTHERSITE_DIALOG_ID:
	    	final CharSequence[] otherSite_items = {"Yes", "No"};
	    	AlertDialog.Builder otherSite_builder = new AlertDialog.Builder(this);
	    	otherSite_builder.setTitle("Logged other locations");
	    	otherSite_builder.setItems(otherSite_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	        //Toast.makeText(getApplicationContext(), delta_items[item], Toast.LENGTH_SHORT).show();
	    	    	otherSite.setText(otherSite_items[item]);
	    	    }
	    	});
	    	AlertDialog otherSite_alert = otherSite_builder.create();
	    	return otherSite_alert;
	    case THISSITE_DIALOG_ID:
	    	final CharSequence[] thisSite_items = {"Yes", "No"};
	    	AlertDialog.Builder thisSite_builder = new AlertDialog.Builder(this);
	    	thisSite_builder.setTitle("Logged this location");
	    	thisSite_builder.setItems(thisSite_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	        //Toast.makeText(getApplicationContext(), delta_items[item], Toast.LENGTH_SHORT).show();
	    	    	thisSite.setText(thisSite_items[item]);
	    	    }
	    	});
	    	AlertDialog thisSite_alert = thisSite_builder.create();
	    	return thisSite_alert;
    	}
    	return null;
    }
    
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
    {		
		public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
		{
            hour = hourOfDay;
            minute = minuteOfHour;  
            
            //set if its AM or PM
            String amPm;
            if(hour > 11)
            	amPm = "PM";
            else
            	amPm = "AM";
            
            //format the hours
            if(hour > 12)
            	hour -= 12;
            if(hour == 0)
            	hour = 12;
            
            //format the minutes
            String zero;
            if(minute < 10)
            	zero = "0";
            else
            	zero = "";
            
            //set the textTime            
            textTime.setText(hour + ":" + zero + minute + " " + amPm);
		}
	};
	
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
    {
         public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
         {
        	 Year = year;
             month = monthOfYear;
             day = dayOfMonth;
             textDate.setText(month + 1 + "/" + day + "/" + Year);
          }
     };
    
	private void iSawFactory(int id, int height, int width, String question, int drawable) 	
	{
		//this section still needs testing.
//	    LinearLayout pyramidLayout = (LinearLayout) findViewById(R.id.pyramid); //this is the base layout ie survey
//	    View rowView = getLayoutInflater().inflate(R.layout.row, null);
//	    LinearLayout rowLayout = (LinearLayout) rowView.findViewById(R.id.row);
		char[] defaultValue = {'N','O','N','E'};
		char[] questionText = question.toCharArray();
		// TODO Auto-generated method stub
		RelativeLayout survey = (RelativeLayout) findViewById(R.id.widget218);
		LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View myView = linflater.inflate(R.layout.survey_element, null);
        
        TextView v = (TextView) myView.findViewById(R.id.Text);
        TextView q = (TextView) myView.findViewById(R.id.question);
        ImageView i = (ImageView) myView.findViewById(R.id.image);
        Button btnExpand = (Button) myView.findViewById(R.id.btnExpand);
        ImageButton btnDwn = (ImageButton) myView.findViewById(R.id.btnDwn);
        ImageButton btnUp = (ImageButton) myView.findViewById(R.id.btnUp);
        
        q.setText(questionText, 0, questionText.length);
        v.setText(defaultValue, 0, defaultValue.length);
        i.setImageResource(drawable);
        
        //there might be an issue with the ID's here during runtime need to look into a better way of doing this.
        v.setId(id*2+id);
        q.setId(id*2*id*2*id*2*id*2 + id);
        i.setId(id*2*id*2 + id);
        btnExpand.setId(id*2*id*2*id*2 + id);
        btnDwn.setId(id*2*id*2*id*2*id*2*id*2 + id);
        btnUp.setId(id*2*id*2*id*2*id*2*id*2*id*2 + id);
        
        btnExpand.setOnClickListener(new View.OnClickListener() {
			
	//		@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
        btnDwn.setImageResource(R.drawable.down_arrow_32);
		btnDwn.setOnClickListener(new View.OnClickListener() {
			
	//		@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		btnUp.setImageResource(R.drawable.up_arrow_32);
		btnUp.setOnClickListener(new View.OnClickListener() {
			
	//		@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
        survey.addView(myView);
		
	}
	private void resetSurvey()
	{
		SurveyGroup.group.reset();
	}
	private void showHelp() {
		Intent intent = new Intent(SurveyActivity.this,About.class);
		About myAbout = new About();
		System.out.println("intent: " + intent);
    	//CommentsActivity.this.startActivity(intent); 
		View v = null;
           // Create the view using FirstGroup's LocalActivityManager 
		try{
			//v = SurveyGroup.group.getLocalActivityManager().startActivity("SurveyAbout",this, myAbout.getClass()).addFlags(intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
		   //System.out.println("new view: " + SurveyGroup.group.getLocalActivityManager().startActivity("show_about", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
           System.out.println("View: " + v);
		   //v = SurveyGroup.group.getLocalActivityManager().startActivity("show_about", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();  
		   System.out.println("View v: " + v);
           // Again, replace the view  
           SurveyGroup.group.replaceView(v);
		}
		catch(NullPointerException e)
		{
			e.printStackTrace();
		}
	}
//	private void inflate(string id) {
//	    LinearLayout pyramidLayout = (LinearLayout) findViewById(R.id.pyramid);
//
//
//	    for (int row = 1 ; row <= mSize; ++row) {
//	        View rowView = getLayoutInflater().inflate(R.layout.row, null);
//	        LinearLayout rowLayout = (LinearLayout) rowView.findViewById(R.id.row);
//	        for (int column = 1; column <= row; ++column) {
//	            View btnView = getLayoutInflater().inflate(R.layout.btn, null);
//	            Button btn = (Button) btnView.findViewById(R.id.button);
//	            btn.setId(row*10 + column);
//	            rowLayout.addView(btnView);
//	        }
//	        pyramidLayout.addView(rowView); 
//	    }
//	}
}
